/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Game/Common.h"


void appendPackGUID(Shared::Binary::ByteBuffer& buffer, std::uint64_t guid)
{
    std::uint8_t packGUID[8 + 1];
    packGUID[0] = 0;
    size_t size = 1;
    for (std::uint8_t i = 0; guid != 0; ++i)
    {
        if (guid & 0xFF)
        {
            packGUID[0] |= std::uint8_t(1 << i);
            packGUID[size] =  std::uint8_t(guid & 0xFF);
            ++size;
        }
        guid >>= 8;
    }
    buffer.append(packGUID, size);
}

std::uint64_t readPackGUID(Shared::Binary::ByteBuffer& buffer)
{
    std::uint64_t guid = 0;
    std::uint8_t guidmark = 0;
    buffer >> guidmark;

    for (int i = 0; i < 8; ++i)
    {
        if (guidmark & (std::uint8_t(1) << i))
        {
            std::uint8_t bit;
            buffer >> bit;
            guid |= (std::uint64_t(bit) << (i * 8));
        }
    }
    return guid;
}